//
//  wilcomeViewController.swift
//  weibo
//
//  Created by Qing on 2017/6/26.
//  Copyright © 2017年 成哥哥. All rights reserved.
//

import UIKit
import SDWebImage

class wilcomeViewController: UIViewController {
    
    //因为UIImageView是继承UIView的，在sb上不能直接这样设置，但用代码可以。 这样设置不用考虑图片的尺寸适应问题。
    override func loadView() {
        view = wilcomeBackImage
    }
    
//MARK: - 控件动画
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        //user头像往上弹的动画
        self.wilcomeUserIcom.snp.updateConstraints { (make) in
            //view.bounds.height是整个屏幕高。- 数为顶端，+150 是顶点往下150.
            make.bottom.equalTo(view.snp.bottom).offset(-view.bounds.height + 150)
        }
        
        //欢迎回来lable 变为透明。
        self.wilcomeLable.alpha = 0
        
        UIView.animate(withDuration: 1,
                       delay: 0,
                       usingSpringWithDamping: 0.8,
                       initialSpringVelocity: 5,
                       options: [], animations: {
                        //通知layoutSubview需要更新约束，
                    self.view.layoutIfNeeded()
                        
        }) { (_) in
            
            //user头像动画结束，欢迎回来动画开始显示。
            UIView.animate(withDuration: 0.3, animations: {
                
                self.wilcomeLable.alpha = 1
                
            }, completion: { (_) in
                
//                self.dismiss(animated: true, completion: { 
//               NotificationCenter.default.post(name: NSNotification.Name.init("WBSwiftRootViewController"), object: "wilcome")
//                })
                //发送监听通知
               NotificationCenter.default.post(name: NSNotification.Name.init("WBSwiftRootViewController"), object: "wilcome")
                
            })
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //欢迎回来用户头像
        wilcomeUserIcom.sd_setImage(with: UserAccountViewModel.sharedUserAccount.avatarUrl, placeholderImage: UIImage(named: "userIcom"))
        setUI()
        
        
    }
    
//MARK: - 懒加载控件
    fileprivate lazy var wilcomeBackImage: UIImageView = UIImageView()
    
    /// 欢迎回来用户头像
    fileprivate lazy var wilcomeUserIcom: UIImageView = {

        let iv: UIImageView = UIImageView(image: UIImage(named: "userIcom"))
        
        //头像显示圆角
        iv.layer.cornerRadius = 45
        iv.layer.masksToBounds = true
        
        return iv
    
    }()
    
    /// 欢迎回来背景图
    fileprivate lazy var wilcomeLable: UILabel = UILabel(texts: "欢迎回来", Color: UIColor.black, fontSize: 18)

}

//MARK: - 界面设置
extension wilcomeViewController {

    fileprivate func setUI() {
        
        wilcomeBackImage.image = UIImage(named: "NewTeaTrue_flower_3")
        view.addSubview(wilcomeUserIcom)
        view.addSubview(wilcomeLable)
    
        
        //约束
        wilcomeUserIcom.snp.makeConstraints { (make) in
            make.centerX.equalTo(view.snp.centerX)
            make.bottom.equalTo(view.snp.bottom).offset(-150)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        wilcomeLable.snp.makeConstraints { (make) in
            make.centerX.equalTo(wilcomeUserIcom.snp.centerX)
            make.top.equalTo(wilcomeUserIcom.snp.bottom).offset(18)
        }
    }
    
}
